package com.quasar.hpatient.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.ada.adapter.RenderAdapter;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.model.custommodel.LifecycleTest;
import com.quasar.hpatient.model.medicalmodel.LifecycleHead;
import com.quasar.hpatient.model.medicalmodel.LifecycleYear;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;
import com.quasar.hpatient.utils.TimeZoneUtil;
import com.quasar.hpatient.view.adapter.render.LifecycleRenderBuiler;
import com.quasar.hpatient.view.prescription.PharmacyRecordActivity_;
import com.quasar.hpatient.view.record.TestRecordActivity_;
import com.quasar.hpatient.widget.MyListView;
import com.vise.log.ViseLog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * name : lishuai
 * time : 2017/10/25
 * title:
 */

public class LifecycleAdapter extends BaseMultiItemQuickAdapter<LifecycleHead, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    PatientInfoData patientInfoData=MainAppliaction.getInstance().getPatient();
    private String Patientid;  //患者ID
    private Context mContext;
    List<LifecycleHead> lifecycleTestList = new ArrayList<>();
    public LifecycleAdapter(Context mContext, List data) {
        super(data);
        this.lifecycleTestList = data;
        this.mContext = mContext;
        addItemType(LifecycleTest.TITLE, R.layout.item_lifecycle_title);  //时间
        addItemType(LifecycleTest.MOUTH, R.layout.item_lifecycle_mouth);  //月
        addItemType(LifecycleTest.CONTENT, R.layout.item_lifecycle_content);  //内容
    }
    public String getPatientid() {
        return Patientid;
    }
    public void setPatientid(String patientid) {
        Patientid = patientid;
    }
    String  title;
    RenderAdapter<LifecycleYear.LifecycleTesmouth> adapter;

    @Override
    protected void convert(BaseViewHolder helper, final LifecycleHead item) {


        switch (helper.getItemViewType()) {
            case LifecycleTest.TITLE:
                String age="未知";
                if(patientInfoData.getBirthday()!=null && patientInfoData.getBirthday().length()>0){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = null;
                    try {
                        date = sdf.parse(patientInfoData.getBirthday());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    age = TimeZoneUtil.getAge(date)+"";

                    if(age.equals("0")){
                        age = 1+"";
                    }
                }
                helper.setText(R.id.tv_age, age);//计算年龄
                helper.setText(R.id.tv_title, item.getYear());
                break;
            case LifecycleTest.CONTENT:
                helper.setText(R.id.tv_month, item.getLifecycleYear().getYear().substring(5, 7));
                MyListView item_lifecyle_listview = helper.getView(R.id.item_lifecyle_listview);
                LifecycleRenderBuiler lifecycleRenderBuiler = new LifecycleRenderBuiler(mContext);
                lifecycleRenderBuiler.setPatientid(getPatientid());
                adapter = new RenderAdapter<>(mContext, lifecycleRenderBuiler);
                adapter.addAll(item.getLifecycleYear().getLifecycleTest());
                item_lifecyle_listview.setAdapter(adapter);

                ViseLog.d("事件显示"+item.getLifecycleYear().getLifecycleTest());
                break;
        }
    }
}
